﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
	internal static class GenParse_UserData_48_ConnectionPath
	{
		#region private constants

		private const int NrOfBytes = 3;

		#endregion private constants

		#region internal methods

		internal static byte[] GenerateBytes(UserData userData)
		{
			byte[] userDataBytes = null;

			if (userData is UserData_48_ConnectionPath)
			{
				UserData_48_ConnectionPath userData48 = userData as UserData_48_ConnectionPath;

				userDataBytes = new byte[NrOfBytes];
				userDataBytes[0] = NrOfBytes - GenParse_UserData.HeaderLength;	// user data length
				userDataBytes[1] = UserData.UserData48ConnectionPath;
				userDataBytes[2] = GenParse_ConnectionPath.GenerateByte(userData48.ConnectionPath);
			}
			else
			{
				// TODO handle error
			}

			return userDataBytes;
		}

		internal static bool ParseBytes(byte[] userDataBytes, out UserData userData48ConnectionPath, out int consumedBytes)
		{
			userData48ConnectionPath = null;
			consumedBytes = 0;

			if (userDataBytes.Length >= NrOfBytes)
			{
				// first byte
				if (userDataBytes[0] == NrOfBytes - GenParse_UserData.HeaderLength)
				{
					if (userDataBytes[1] == UserData.UserData48ConnectionPath)
					{
						eConnectionPath conPath;
						bool bRetVal = GenParse_ConnectionPath.ParseByte(userDataBytes[2], out conPath);
						if (bRetVal)
						{
							userData48ConnectionPath = new UserData_48_ConnectionPath(conPath);
							consumedBytes = NrOfBytes;

							return true;
						}

						// TODO handle error - wrong byte value for eHistory
						return false;
					}

					// wrong value for the data type
					return false;
				}

				// TODO handle error (wrong value in first byte the length)
				return false;
			}

			// TODO handle error (not enough bytes)
			return false;
		}

		#endregion internal methods
	}
}
